const app = getApp()
import {register , sendMsg} from '../../api/user'
import { hexMD5 } from "../../utils/md5.js"
Page({

  data: {
    verifyCodeKey: '',
    lineShow: true,
    phoneNumber: '',
    isShow: true,
    vCode:'',
    SHARE_TYPE:0,
    xuid:null,
    ivcode:'',
    isShow: true,
    second: 60,
    verifyCode:''
  },
  onLoad: function (options) {
    this.data.xuid=app.xuid
    this.setData({
      xuid:this.data.xuid
    })
  },
  verifyCodeChange(e) {
    this.data.verifyCode = e.detail.value
  },
  timer: function () {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(
        () => {
          this.setData({
            second: this.data.second - 1
          })
          if (this.data.second <= 0) {
            this.setData({
              second: 60,
              isShow: true,
            })
            resolve(setTimer)
          }
        }, 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },
  verificationClick() {
    if (this.data.phoneNumber == "" || this.data.phoneNumber.length != 11 || !(/^((1))\d{10}$/.test(this.data.phoneNumber))) {
      wx.showToast({
        title: '请输入正确手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    this.timer()
    sendMsg({
      username: this.data.phoneNumber,
            tplKey: 'register',
      mchId: app.ISV_ID,
    }).then(res => {

    })
    this.data.isShow = false
    this.setData({
      isShow: this.data.isShow
    })
  },
  loginClick() {
    var _this = this;
    if(this.data.phoneNumber == ""){
        wx.showToast({
          title: '账号不能为空',
          icon: 'none',
        })
        return false
      }
    if(this.data.vCode == ""){
      wx.showToast({
        title: '密码不能为空',
        icon: 'none',
      })
      return false
    }
    if(this.data.ivcode != this.data.vCode){
      wx.showToast({
        title: '确认密码不一致，请重新输入。',
        icon: 'none',
      })
      return false
    }
    if(this.data.verifyCode == ""){
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none',
      })
      return false
    }
      register({
        mchId: app.globalData.mchId,
        mobile: this.data.phoneNumber,
        password: hexMD5(this.data.vCode),
        verifyCode:this.data.verifyCode
      }).then(res => {
        if (res.type == 'success') {
          const {
            userInfo,
            token
          } = res.data
            app.globalData.token = token
            app.globalData.userInfo = userInfo
            wx.switchTab({
              url: '/pages/my/my'
            })
        }
      }).catch(error => {
        wx.showToast({
          title: error.content,
          icon: 'none',
          duration: 2000
        })
      })
  },
  inputInvitationNum(e) {
    this.data.ivcode = e.detail.value
    this.setData({
      ivcode: this.data.ivcode
    })
  },
  inputPhoneNum(e) {
    this.data.phoneNumber = e.detail.value
    this.setData({
      phoneNumber: this.data.phoneNumber
    })
  },
  verificationCode(e) {
    this.data.vCode = e.detail.value
  },
})
